package net.qanar.gwnewsfeed.reader;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.*;

import net.qanar.gwnewsfeed.*;

public class IGUReader extends Reader {
	private static String link = "http://wiki.guildwars.com/index.php?title=ArenaNet:Login_screen_announcements&printable=yes";
	private Collection<Item> items;
	
	{
		this.items = new ArrayList<Item>();
		
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new URL(link).openStream()));
			String line = br.readLine();
			String content = "";
			while (line != null) {
				if (line.startsWith("<a name=\"Current_announcements\"></a>")) {
					line = br.readLine();
					while(true) {
						line = line.replace("<a href=\"/wiki/", "<a href=\"http://www.wiki.guildwars.com/wiki/");
						line = line.replace("src=\"/images/", "src=\"http://www.wiki.guildwars.com/images/");
						if (line.contains("edit</a>]")) {
							line = line.substring(0, line.indexOf('[')) +
									line.substring(line.indexOf(']')+1);
						}
						if (line != null && line.contains("See also </span></h2>"))
							break;
						content += "\n" + line;
						line = br.readLine();
					}
				}
				if (line != null && line.contains("See also </span></h2>"))
					break;			
				line = br.readLine();
			}
			items.add(new Item(getName(), content, getName(), new Date()));
		} catch (Exception e) {e.printStackTrace();}
	}
	
	@Override
	public Collection<Item> getItems() {
		return items;
	}

	@Override
	public String getName() {
		return "In-game Announcements";
	}

}
